import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { LikeVideo } from './likeVideo.entity';
import { LikeVideoService } from './likeVideo.service';

import {join, resolve} from 'path'
import { createWriteStream } from 'fs';


import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { LikeVideoSourceEnum } from './likeVideo.enum';
@Controller(apibefore('/likeVideo'))
export class LikeVideoController {
  constructor(private readonly likeVideoService: LikeVideoService) {}


  @Post('/create')
  @UseInterceptors(ValidTokenInterceptor)
  @UseInterceptors(ValidRSAInterceptor)
  async create(@Body() likeVideoParams: { video_id: number, source?: string, method?: boolean, main?: boolean, shareuid?: number},@Request() req) {
    if(isNaN(Number(likeVideoParams.video_id))){
      return new HttpException('非法传参',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    //查询视频是否存在
    let isExists = await this.likeVideoService.isCreate(likeVideoParams.video_id)
    if(isExists?.error){
      return new HttpException(isExists.error,HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let wx_user = req.headers.wx_user
    if(!likeVideoParams.method){
      let error = await this.likeVideoService.remove_by_video_id(likeVideoParams.video_id, wx_user)
      if(error){
        return new HttpException(error,HttpStatus.INTERNAL_SERVER_ERROR)
      }else{
        return true
      }
    }else{
      const likeVideo = new LikeVideo()
      likeVideo.wx_user = wx_user
      likeVideo.like_count = 1
      likeVideo.video_id = likeVideoParams.video_id
      likeVideo.last_time = new Date()
      if(likeVideoParams.main){
        likeVideo.source = LikeVideoSourceEnum.main
      }else if(likeVideoParams.source === 'ic_info' || likeVideoParams.source == "1"){
        likeVideo.source = LikeVideoSourceEnum.ic_info
      }else{
        likeVideo.source = LikeVideoSourceEnum.other
      }
      return await this.likeVideoService.create(likeVideo, likeVideoParams.shareuid)
    }
  }

  @Post('/update')
  update(@Body() likeVideoParams: { id: number; title: string; desc: string, wx_user: string, gm_user: number,video_url: string }) {

  }

  @Get('/findAll')
  async findAll() {

  }

  @Post('/remove')
  async remove(@Body() likeVideoParams: { id: number }){
    return await this.likeVideoService.remove(likeVideoParams.id)
  }

  deleteVideo(FileId: string, then: Function, catchF: Function){

  }
}
